/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Jul 31, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.data;

import edu.psu.geovista.data.geog.DataSetForApps;
import edu.psu.geovista.data.model.DefaultPlotModel;
import edu.psu.geovista.data.model.PlotModel;
import edu.psu.geovista.io.csv.CSVFileDataReader;

import java.util.ArrayList;

public class DataSource {
    String dataFileName;
    DataSetForApps dataSet;


    public DataSource() {
    }

    public void setDataFileName(String dataFileName) {
        this.dataFileName = dataFileName;
        this.loadData(dataFileName);
    }

    private void loadData(String fileName) {
       CSVFileDataReader csvR=new CSVFileDataReader();
       csvR.setFileName(fileName);
       dataSet=csvR.getDataForApps() ;

    }
    /**
     *
     * @param dataName
     * @return  a set of data related to the given attribute in term of time series
     */
    public edu.psu.geovista.data.model.AppDataModel getTimeSeriesData(String dataName){
       PlotModel dataModel=DefaultPlotModel.createDefaultPlotModel();
        String[] attributeNames=this.dataSet.getAttributeNamesNumeric() ;
        ArrayList indexs=new ArrayList();
        for (int i=0;i<attributeNames.length ;i++){
            String name=attributeNames[i];
            if(name.startsWith("pop") ){
                indexs.add(new Integer(i));
            }

        }
        Object[] idxs=indexs.toArray() ;

        for (int i=0;i<idxs.length ;i++){
            int index=((Integer)idxs[i]).intValue() ;
            String attName=attributeNames[index];
            double[] att=dataSet.getNumericDataAsDouble(index);
            dataModel.addColumn(attName,att);
        }

        //set column value
        dataModel.addColumnValues("pop90",90);
        dataModel.addColumnValues("pop94",94);
        dataModel.addColumnValues("pop95",95);
        dataModel.addColumnValues("pop96",96);

        return dataModel;
    }

}
